package org.tloss.web.editor.model.oper.logical;

import org.tloss.web.editor.utils.Constants;

public class ComplexLogical extends Logical {
	Logical logical1;
	Logical logical2;
	LogicalOperator operator;

	public ComplexLogical() {

	}

	public ComplexLogical(Logical logical1, Logical logical2,
			LogicalOperator operator) {
		this.logical1 = logical1;
		this.logical2 = logical2;
		this.operator = operator;
	}

	public Logical getLogical1() {
		return logical1;
	}

	public void setLogical1(Logical logical1) {
		this.logical1 = logical1;
	}

	public Logical getLogical2() {
		return logical2;
	}

	public void setLogical2(Logical logical2) {
		this.logical2 = logical2;
	}

	public LogicalOperator getOperator() {
		return operator;
	}

	public void setOperator(LogicalOperator operator) {
		this.operator = operator;
	}

	@Override
	public String getDisplayText() {
		if (operator.supportTwoOperand())
			return "(" + logical1.getDisplayText() + " "
					+ operator.getDisplayText() + " "
					+ logical2.getDisplayText() + ")";
		else if (!operator.supportTail()) {
			return "" + operator.getDisplayText() + "("
					+ logical1.getDisplayText() + ")";
		} else {
			return "" + logical1.getDisplayText() + " "
					+ operator.getDisplayText();
		}
	}

	@Override
	public String generate(int type, Object[] data) {
		if (type == Constants.CodeType.JAVA) {
			if (operator.supportTwoOperand())
				return "(" + logical1.generate(type, data) + " "
						+ operator.generate(type, data) + " "
						+ logical2.generate(type, data) + ")";
			else if (!operator.supportTail()) {
				return "" + operator.generate(type, data) + "("
						+ logical1.generate(type, data) + ")";
			} else {
				return "" + logical1.generate(type, data) + " "
						+ operator.generate(type, data);
			}
		}
		return getDisplayText();

	}

}
